/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "songrequestwidget.h"

#include <QWidget>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QSlider>
#include <QTimer>

#include <QList>
#include <QPair>

#include <QResizeEvent>

#include <QDebug>

#include "votewidget.h"

#include "utils.h"

SongRequestWidget :: SongRequestWidget(QWidget* parent/*=0*/)
    :QWidget(parent),isConnected(false)
{
    pMainGroupBox = new QGroupBox(tr("Current song"),this);
    pMainGroupBox->setAlignment(Qt::AlignHCenter);

    this->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,QSizePolicy::Maximum));
    pMainGroupBox->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,QSizePolicy::Maximum));

    pMainVGrid = new QVBoxLayout(pMainGroupBox);
    pTitleLabel = new QLabel(tr("No Media"),this);

    pMainVGrid->addWidget(pTitleLabel,0,Qt::AlignHCenter);

    pSongLengthLabel = NULL;

    pRequesterHGrid = NULL;
    pRequesterTitle = NULL;
    pFlagRequesterLabel = NULL;
    pRequesterLabel = NULL;

    pVoteWidget = new VoteWidget(this);
    pVoteWidget->hide();

    pProgressionHGrid =NULL;

    pTimeLeftLabel = NULL;
    pSongLengthLabel = NULL;

    pProgressionSlider = NULL;

    pCountDownTimer = new QTimer(this);
    pCountDownTimer->setInterval(1000); // All seconds

    connect(pCountDownTimer, SIGNAL(timeout()), this, SLOT(updateCountDown()));

    //this->updateInterface();

    qDebug() << "SongRequestWidget :: SongRequestWidget";
}

SongRequestWidget :: ~SongRequestWidget(void)
{
    this->clearGrid();

    qDebug() << "SongRequestWidget :: deleted";
}

void SongRequestWidget :: resizeEvent ( QResizeEvent * event )
{
    qDebug() << "SongRequestWidget :: resizeEvent" << event->size();
    pMainGroupBox->resize(event->size());
    event->accept();
}

void SongRequestWidget :: clearGrid(void)
{
    qDebug() << "SongRequestWidget :: clearGrid()";

    pMainVGrid->removeWidget(pTitleLabel);

    for ( QList<QPair<QLabel*,QLabel*> >::iterator it_artists = pListArtist.begin() ; it_artists != pListArtist.end() ; it_artists++ )
    {
        delete it_artists->first;
        delete it_artists->second;
    }

    pListArtist.clear();

    for ( QList<QHBoxLayout*>::const_iterator itPHBox = pListPHArtistGrid.begin() ; itPHBox != pListPHArtistGrid.end() ; itPHBox++ )
    {
        pMainVGrid->removeItem(*itPHBox);
        delete (*itPHBox);
    }

    pListPHArtistGrid.clear();

    pMainVGrid->removeItem(pRequesterHGrid);

    if ( isConnected )
    {
        pMainVGrid->removeWidget(pVoteWidget);
        pVoteWidget->hide();
    }

    pMainVGrid->removeItem(pProgressionHGrid);

    pMainVGrid->removeWidget(pProgressionSlider);
}

void SongRequestWidget :: updateInterface(void)
{
    this->clearGrid();

    // The creation of the interface is done only here to avoid to display the widget before having the data
    if ( data.artists.size() != 0 && pProgressionSlider == NULL )
    {
        pRequesterHGrid = new QHBoxLayout();
        pRequesterTitle = new QLabel(tr("Requester: "),this);
        pFlagRequesterLabel = new QLabel(this);
        pRequesterLabel = new QLabel(this);

        pRequesterHGrid->addWidget(pRequesterTitle,0,Qt::AlignLeft);
        pRequesterHGrid->addWidget(pFlagRequesterLabel,0,Qt::AlignLeft);
        pRequesterHGrid->addWidget(pRequesterLabel,100,Qt::AlignLeft);

        pProgressionHGrid = new QHBoxLayout();

        pTimeLeftLabel = new QLabel(this);
        pSongLengthLabel = new QLabel(this);

        pProgressionHGrid->addWidget(pTimeLeftLabel,0,Qt::AlignRight);
        pProgressionHGrid->addWidget(pSongLengthLabel,0,Qt::AlignLeft);

        pProgressionSlider = new QSlider(Qt::Horizontal,this);
        pProgressionSlider->setEnabled(false);

        pCountDownTimer->start();
    }

    // Fill data
    if ( !this->data.songInfo.title.isEmpty() )
    {
        idSong = data.songInfo.id;
        pTitleLabel->setText(QString("<b>") + this->data.songInfo.title + QString("</b>"));
    }
    else
    {
        pTitleLabel->setText(tr("No Media") + this->data.songInfo.title);
    }
    pMainVGrid->addWidget(pTitleLabel,0,Qt::AlignHCenter);

    // Fill grid
    // this->pMainGrid->addWidget(pTitle,0,1,Qt::AlignHCenter);

    for ( QList<Artist>::iterator it_artist = this->data.artists.begin() ; it_artist != this->data.artists.end() ; it_artist++ )
    {
        QLabel* pIconFlagLabel = new QLabel();
        QLabel* pArtistNameLabel = new QLabel(it_artist->nickname);
        QHBoxLayout* pHBox = new QHBoxLayout();

        pIconFlagLabel->setPixmap(QPixmap(Utils::getFlagPathFromName(it_artist->flag)));
        pArtistNameLabel->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);

        this->pListArtist.push_back(QPair<QLabel*,QLabel*>(pIconFlagLabel,pArtistNameLabel));

        pHBox->addWidget(pIconFlagLabel,20,Qt::AlignRight);
        pHBox->addWidget(pArtistNameLabel,0,Qt::AlignRight);
        pHBox->addSpacing(this->width()/5);

        pMainVGrid->addLayout(pHBox);
    }


    if ( !data.requesterInfo.name.isEmpty() )
    {
        pFlagRequesterLabel->setPixmap(QPixmap(Utils::getFlagPathFromName(this->data.requesterInfo.flag)));
        pRequesterLabel->setText(this->data.requesterInfo.name);

        pMainVGrid->addLayout(pRequesterHGrid);
    }

    if ( isConnected )
    {
        pVoteWidget->reset();
        pMainVGrid->addWidget(pVoteWidget,0,Qt::AlignHCenter);
        pVoteWidget->show();
    }

    if ( !data.songInfo.title.isEmpty() )
    {
        // Calculation to have the ellapsed time from the remaining time
        unsigned int ellapsedSeconds = (data.songInfo.length.second() + data.songInfo.length.minute()*60)  - data.timeLeft;
        QTime ellapsedTime = QTime(0,ellapsedSeconds/60,ellapsedSeconds%60);

        pTimeLeftLabel->setText(ellapsedTime.toString("m:ss") + QString("   /"));
        pSongLengthLabel->setText(data.songInfo.length.toString("m:ss"));

        pProgressionSlider->setRange(0,data.songInfo.length.second() + data.songInfo.length.minute()*60);
        pProgressionSlider->setValue(ellapsedSeconds);

        pMainVGrid->addLayout(pProgressionHGrid);
        pMainVGrid->addWidget(pProgressionSlider);
    }

    this->updateGeometry();
}

void SongRequestWidget :: setData(const SongRequest& songRequestData)
{
    qDebug() << "SongRequestWidget :: setData";

    this->data = songRequestData;

    this->updateInterface();
}

void SongRequestWidget :: updateCountDown()
{
    qDebug() << "SongRequestWidget :: updateCountDown";

    if ( data.timeLeft > 0 )
    {
        unsigned int ellapsedSeconds = (data.songInfo.length.second() + data.songInfo.length.minute()*60)  - data.timeLeft;
        QTime ellapsedTime = QTime(0,ellapsedSeconds/60,ellapsedSeconds%60);

        data.timeLeft--;

        pTimeLeftLabel->setText(ellapsedTime.toString("m:ss") + QString("   /"));
        pProgressionSlider->setValue(ellapsedSeconds);
    }
}

QSize SongRequestWidget :: sizeHint(void)
{
    return this->pMainGroupBox->sizeHint();
}

QSize SongRequestWidget :: minimumSizeHint()const
{
    return this->pMainGroupBox->minimumSizeHint();
}

void SongRequestWidget :: setConnected(const bool isConnected)
{
    qDebug() << "SongRequestWidget :: setConnected (" << isConnected << ")";

    this->isConnected = isConnected;

    this->updateInterface();
}
